package session;

import java.util.concurrent.ConcurrentHashMap;


public class SessionDataBlock {
	
	//all the session data
	private ConcurrentHashMap<String,SessionData> sessionData;
	//when the session data block times out
	private Long expiration;
	
	//constructor
	public SessionDataBlock(Long expiration){
		sessionData = new ConcurrentHashMap<String,SessionData>();
		this.expiration=expiration;
	}
	
	public void removeSession(String sessionID){
		sessionData.remove(sessionID);
	}
	
	//getter
	public Long getExpiration(){
		return expiration;
	}
	
	//getter
	public SessionData getSession(String sessionID){
		return sessionData.get(sessionID);
	}
	
	//setter
	public ErrorCodes setSession(SessionData session){
		//dont allow old sessions to replace new ones
		if(sessionData.get(session.sessionID)==null || sessionData.get(session.sessionID).version<=session.version){
			sessionData.put(session.sessionID, session);
			return ErrorCodes.SUCCESS;
		} else {
			return ErrorCodes.OLD_VERSION;
		}
	}
}
